<template>
  <div class="app-container">
    <h4 class="form-header h4">文章信息</h4>
<el-form ref="detailForm" :model="detailForm" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="标题" prop="title">
            <el-input v-model="detailForm.title" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="发布时间" prop="releaseTime">
            <el-input  v-model="detailForm.releaseTime" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col :span="24">
          <div v-html="detailForm.content"></div>
        </el-col>
      </el-row> -->
    </el-form>

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd()">评论</el-button>
      </el-form-item>
    </el-form>
    <h4 class="form-header h4">评论信息</h4>
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="父id" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章id" prop="detailId">
        <el-input
          v-model="queryParams.detailId"
          placeholder="请输入文章id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
	    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form> -->

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:detailReview:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="detailReviewList"
      row-key="reviewId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
     <!-- <el-table-column label="父id" prop="parentId" /> -->
      <el-table-column label="评论内容" align="left" prop="content" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{scope.row.content}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="文章id" align="center" prop="detailId" />
       -->
       <el-table-column label="评论者" align="left" prop="nickName" />
       <el-table-column label="评论时间" align="left" prop="createTime" />
       <el-table-column label="类型" align="center" prop="reviewType">
         <template slot-scope="scope">
             <dict-tag :options="dict.type.tb_review_type" :value="scope.row.reviewType"/>
           </template>
       </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['business:detailReview:edit']"
          >评论设置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >评论</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:detailReview:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改用户文章评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="评论对象" prop="parentId" v-if="form.parentId != 0">
          <span>{{form.parentContent}}</span>
        </el-form-item>
        <el-form-item label="评论内容">
          <el-input v-model="form.content" type="textarea" rows="5"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="神评论设置" :visible.sync="openForm" width="900px" append-to-body>
      <el-form ref="plForm" :model="plForm" label-width="80px">
        <el-form-item label="评论对象" prop="parentId">
          <span>{{plForm.content}}</span>
        </el-form-item>
        <el-form-item prop="reviewType" label="评论类型">
          <el-radio-group v-model="plForm.reviewType">
            <el-radio
              v-for="dict in dict.type.tb_review_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetailReview, getDetailReview, delDetailReview, addDetailReview, updateDetailReview } from "@/api/business/detailReview";
import { getColumnDetail } from "@/api/business/columnDetail";

export default {
  name: "DetailReview",
  dicts: ['tb_review_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 用户文章评论表格数据
      detailReviewList: [],
      // 用户文章评论树选项
      detailReviewOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        parentId: null,
        content: null,
        detailId: null,
        userId: null,
        reviewType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      detailForm: {
        title: null,
        releaseTime: null
      },
      detailId: null,
      openForm: false,
      plForm: {
        reviewId: null,
        content: null,
        reviewType: '0'
      }
    };
  },
  created() {
    this.initData();
  },
  methods: {
    handleDetail(row){
      this.plForm = {
        reviewId: row.reviewId,
        content: row.content,
        reviewType: row.reviewType + ''
      }
      this.openForm = true
    },
    initData(){
      const that = this;
      let data = this.$route.query;
      if(data.detailId != undefined && data.detailId != this.detailId){
        that.detailId = data.detailId
        this.queryParams.detailId = data.detailId;
        this.getDetail(data.detailId)
      }
      this.getList();
    },
    getDetail(detailId){
      getColumnDetail(detailId).then(response => {
        this.detailForm = response.data;
      });
    },
    /** 查询用户文章评论列表 */
    getList() {
      this.loading = true;
      listDetailReview(this.queryParams).then(response => {
        this.detailReviewList = this.handleTree(response.data, "reviewId", "parentId");
        this.loading = false;
      });
    },
    /** 转换用户文章评论数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.reviewId,
        label: node.content,
        children: node.children
      };
    },
	/** 查询用户文章评论下拉树结构 */
    getTreeselect() {
      listDetailReview().then(response => {
        this.detailReviewOptions = [];
        const data = { reviewId: 0, content: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "reviewId", "parentId");
        this.detailReviewOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.openForm = false;
      this.plForm = {
        reviewId: null,
        content: null,
        reviewType: '0'
      }
      this.resetForm("plForm");
    },
    // 表单重置
    reset() {
      this.form = {
        reviewId: null,
        parentId: 0,
        content: null,
        detailId: this.queryParams.detailId,
        parentContent: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      //this.getTreeselect();
      if (row != null && row.reviewId) {
        this.form.parentId = row.reviewId;
        this.form.parentContent = row.content
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "文章评论";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.parentId = row.parentId;
      }
      getDetailReview(row.reviewId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户文章评论";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["plForm"].validate(valid => {
        if (valid) {
          if (this.plForm.reviewId != null) {
            updateDetailReview(this.plForm).then(response => {
              this.$modal.msgSuccess("设置成功");
              this.openForm = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitForm1() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.reviewId == null) {
            addDetailReview(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除用户文章评论编号为"' + row.reviewId + '"的数据项？').then(function() {
        return delDetailReview(row.reviewId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
